package com.cenntro.common.util.result;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

public class Result implements Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUId = -5504174446258175969L;
	public static final Integer SUCCESS = 1;
	public static final Integer ERROR = -9;
	
	/**
	 * 返回是否成功的状态 1:表示成功 -9或其他值代表失败
	 */
	private int state = 1;
	/**
	 * 成功时,返回的JSON数据
	 */
	private Object data;
	/**
	 * 提示信息
	 */
	private String message;
	public Result() {
	}
	public Result(int state, Object data, String message) {
		this.state = state;
		this.data = data;
		this.message = message;
	}

	public Result(int state, Throwable e) {
		this.state = state;
		data = null;
		message = e.getMessage();
	}

	public static Result ok(Object data) {
		return new Result(SUCCESS, data, "SUCCESS");
	}
	public static Result ok(Object data, String msg) {
		return new Result(SUCCESS, data, msg);
	}
	public static Result ok() {
		return new Result(SUCCESS, null, "SUCCESS");
	}
	
	public static Result error(String msg) {
		return new Result(ERROR, "", msg);
	}
	
	public static Result error(String msg, Object data) {
		return new Result(ERROR, data, msg);
	}
	
	public static Result error(int state, String msg) {
		return new Result(state, "", msg);
	}
	

	public Integer getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String toString() {
		return "result [state=" + state + ", data=" + JSON.toJSONString(data) + ", message=" + message + "]";
	}
	
	public static void main(String[] args) {
		System.out.println(Result.SUCCESS);
	}

}
